/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int add = 0;//存储进位
        ListNode head = new ListNode(0);
        ListNode ans = head;
        while(l1 != null || l2 != null || add != 0){
            int x = l1 != null ? l1.val : 0;
            int y = l2 != null ? l2.val : 0;
            int temp = x + y + add;
            add = temp / 10;
            ans.next = new ListNode(temp % 10);

            ans = ans.next;
            if(l1 != null)  l1 = l1.next;
            if(l2 != null)  l2 = l2.next;
        }

        return head.next;
    }
}